home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_337 / cmanual / gadgets.lzh / Gadgets / Example10.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  9KB  |  283 lines

  1. /* Example10                                                          */
  2. /* This program will open a normal window which is connected to the   */
  3. /* Workbench Screen. The window will use all System Gadgets, and will */
  4. /* close first when the user has selected the System gadget Close     */
  5. /* window. Inside the window we have put a Proportional gadget which  */
  6. /* uses a custom image knob.                                          */ 
  7.  
  8.  
  9.  
  10. #include <intuition/intuition.h>
  11.  
  12.  
  13.  
  14. struct IntuitionBase *IntuitionBase;
  15.  
  16.  
  17.  
  18. /* THE PROPORTIONAL GADGET's STRUCTURES: */
  19.  
  20. /* The IntuiText structure: */
  21. struct IntuiText my_text=
  22. {
  23.   1,         /* FrontPen, colour register 1. */
  24.   0,         /* BackPen, colour register 0. */
  25.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  26.              /* change the background. */ 
  27.   -65, 8,    /* LeftEdge, TopEdge. */
  28.   NULL,      /* ITextFont, use default font. */
  29.   "Volume:", /* IText, the text that will be printed. */
  30.   NULL,      /* NextText, no other IntuiText structures. */
  31. };
  32.  
  33.  
  34. /* Image data for the knob: */
  35. /* Remember that Image data must ALWAYS be in chip memory! */
  36. USHORT chip my_knob_data[80]=
  37. {
  38.   0x01E0,0x0000, /* Bitplane ZERO */
  39.   0x03F0,0x0000,
  40.   0x03F0,0x0000,
  41.   0x03F0,0x0000,
  42.   0x03F0,0x0000,
  43.   0x03F0,0x0000,
  44.   0x03F0,0x0000,
  45.   0x07F8,0x0000,
  46.   0x7BF7,0x8000,
  47.   0x83F0,0x4000,
  48.   0x83F0,0x4000,
  49.   0x7BF7,0x8000,
  50.   0x07F8,0x0000,
  51.   0x03F0,0x0000,
  52.   0x03F0,0x0000,
  53.   0x03F0,0x0000,
  54.   0x03F0,0x0000,
  55.   0x03F0,0x0000,
  56.   0x03F0,0x0000,
  57.   0x01E0,0x0000,
  58.  
  59.   0x0000,0x0000, /* Bitplane ONE */
  60.   0x01E0,0x0000,
  61.   0x01E0,0x0000,
  62.   0x01E0,0x0000,
  63.   0x01E0,0x0000,
  64.   0x01E0,0x0000,
  65.   0x01E0,0x0000,
  66.   0x01E0,0x0000,
  67.   0x05E8,0x0000,
  68.   0x7DEF,0x8000,
  69.   0x7DEF,0x8000,
  70.   0x05E8,0x0000,
  71.   0x01E0,0x0000,
  72.   0x01E0,0x0000,
  73.   0x01E0,0x0000,
  74.   0x01E0,0x0000,
  75.   0x01E0,0x0000,
  76.   0x01E0,0x0000,
  77.   0x01E0,0x0000,
  78.   0x0000,0x0000
  79. };
  80.  
  81. /* The Image structure for the knob: */
  82. struct Image my_knob=
  83. {
  84.   0, 0,         /* LeftEdge, TopEdge */
  85.   18, 20,       /* Width, Height */
  86.   2,            /* Depth */
  87.     my_knob_data, /* ImageData */
  88.   0x03, 0x00,   /* PlanePick, PlaneOnOff */
  89.   NULL          /* NextImage */
  90. };
  91.  
  92.  
  93. struct PropInfo my_prop_info=
  94. {
  95.   FREEHORIZ,      /* Flags, the knob should be moved horizontally. */
  96.   0,              /* HorizPot, start position of the knob. */
  97.   0,              /* VertPot, 0 since we will not move the knob hor. */
  98.   MAXBODY * 1/64, /* HorizBody, 64 steps. */
  99.   0,              /* VertBody, 0 since we will not move the knob hor. */
  100.  
  101.   /* These variables are initialized and maintained by Intuition: */
  102.  
  103.   0,              /* CWidth */
  104.   0,              /* CHeight */
  105.   0, 0,           /* HPotRes, VPotRes */
  106.   0,              /* LeftBorder */
  107.   0               /* TopBorder */
  108. };
  109.  
  110.  
  111. struct Gadget my_gadget=
  112. {
  113.   NULL,            /* NextGadget, no more gadgets in the list. */
  114.   80,              /* LeftEdge, 80 pixels out. */
  115.   30,              /* TopEdge, 30 lines down. */
  116.   200,             /* Width, 200 pixels wide. */
  117.   24,              /* Height, 24 pixels lines heigh. */
  118.   GADGHNONE,       /* Flags, no highlightning. */
  119.   GADGIMMEDIATE|   /* Activation, our program will recieve a message */
  120.   RELVERIFY,       /* when the user has selected this gadget, and when */
  121.                    /* the user has released it. */ 
  122.   PROPGADGET,      /* GadgetType, a Proportional gadget. */
  123.   (APTR) &my_knob, /* GadgetRender, a pointer to our knob Image str. */
  124.   NULL,            /* SelectRender, NULL since we do not supply the */
  125.                    /* gadget with an alternative image. */
  126.   &my_text,        /* GadgetText, volume. */
  127.   NULL,            /* MutualExclude, no mutual exclude. */
  128.   (APTR) &my_prop_info, /* SpecialInfo, pointer to a PropInfo structure. */
  129.   0,               /* GadgetID, no id. */
  130.   NULL             /* UserData, no user data connected to the gadget. */
  131. };
  132.  
  133.  
  134.  
  135. /* Declare a pointer to a Window structure: */ 
  136. struct Window *my_window;
  137.  
  138. /* Declare and initialize your NewWindow structure: */
  139. struct NewWindow my_new_window=
  140. {
  141.   50,            /* LeftEdge    x position of the window. */
  142.   25,            /* TopEdge     y positio of the window. */
  143.   320,           /* Width       320 pixels wide. */
  144.   100,           /* Height      100 lines high. */
  145.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  146.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  147.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  148.                  /*             user has selected the Close window gad, */
  149.   GADGETDOWN|    /*             or a gadget has been pressed on, or */
  150.   GADGETUP,      /*             a gadge has been released. */
  151.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  152.   WINDOWCLOSE|   /*             Close Gadget. */
  153.   WINDOWDRAG|    /*             Drag gadget. */
  154.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  155.   WINDOWSIZING|  /*             Sizing Gadget. */
  156.   ACTIVATE,      /*             The window should be Active when opened. */
  157.   &my_gadget,    /* FirstGadget A pointer to the String gadget. */
  158.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  159.   "Proportional Window", /* Title Title of the window. */
  160.   NULL,          /* Screen      Connected to the Workbench Screen. */
  161.   NULL,          /* BitMap      No Custom BitMap. */
  162.   320,           /* MinWidth    We will not allow the window to become */
  163.   60,            /* MinHeight   smaller than 320 x 60, and not bigger */
  164.   640,           /* MaxWidth    than 640 x 200. */
  165.   200,           /* MaxHeight */
  166.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  167. };
  168.  
  169.  
  170.  
  171. main()
  172. {
  173.   /* Boolean variable used for the while loop: */
  174.   BOOL close_me;
  175.  
  176.   /* Declare a variable in which we will store the IDCMP flag: */
  177.   ULONG class;
  178.  
  179.   /* Declare a pointer to an IntuiMessage structure: */
  180.   struct IntuiMessage *my_message;
  181.  
  182.  
  183.  
  184.   /* Before we can use Intuition we need to open the Intuition Library: */
  185.   IntuitionBase = (struct IntuitionBase *)
  186.     OpenLibrary( "intuition.library", 0 );
  187.   
  188.   if( IntuitionBase == NULL )
  189.     exit(); /* Could NOT open the Intuition Library! */
  190.  
  191.  
  192.  
  193.   /* We will now try to open the window: */
  194.   my_window = (struct Window *) OpenWindow( &my_new_window );
  195.   
  196.   /* Have we opened the window succesfully? */
  197.   if(my_window == NULL)
  198.   {
  199.     /* Could NOT open the Window! */
  200.     
  201.     /* Close the Intuition Library since we have opened it: */
  202.     CloseLibrary( IntuitionBase );
  203.  
  204.     exit();  
  205.   }
  206.  
  207.  
  208.  
  209.   /* We have opened the window, and everything seems to be OK. */
  210.  
  211.  
  212.  
  213.   close_me = FALSE;
  214.  
  215.   /* Stay in the while loop until the user has selected the Close window */
  216.   /* gadget: */
  217.   while( close_me == FALSE )
  218.   {
  219.     /* Wait until we have recieved a message: */
  220.     Wait( 1 << my_window->UserPort->mp_SigBit );
  221.  
  222.     /* We have now recieved one or more messages. */
  223.  
  224.     /* Since we may recieve several messages we stay in the while loop */
  225.     /* and collect, save, reply and execute the messages until there is */
  226.     /* a pause: */
  227.     while(my_message=(struct IntuiMessage *)GetMsg( my_window->UserPort))
  228.     {
  229.       /* GetMsg will return a pointer to a message if there was one, */
  230.       /* else it returns NULL. We will therefore stay in this while loop */
  231.       /* as long as there are some messages waiting in the port. */
  232.       
  233.       /* After we have collected the message we can read it, and save */
  234.       /* any important values which we maybe want to check later: */
  235.       class = my_message->Class;      /* Save the IDCMP flag. */
  236.  
  237.       /* After we have read it we reply as fast as possible: */
  238.       /* REMEMBER! Do never try to read a message after you have replied! */
  239.       /* Some other process has maybe changed it. */
  240.       ReplyMsg( my_message );
  241.  
  242.       /* Check which IDCMP flag was sent: */
  243.       switch( class )
  244.       {
  245.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  246.                close_me=TRUE;
  247.                break;
  248.              
  249.         case GADGETDOWN:   /* The user has selected the Prop. gadget: */
  250.                printf("Proportional gadget selected.\n");
  251.                break;
  252.              
  253.         case GADGETUP:     /* The user has released the Prop. gadget: */
  254.                printf("Proportional gadget released.\n");
  255.                break;
  256.       }
  257.     }
  258.     printf("Volume= %1.0f\n\n", (float) my_prop_info.HorizPot/MAXPOT*64);
  259.   }
  260.  
  261.   /* We should always close the windows we have opened before we leave: */
  262.   CloseWindow( my_window );
  263.  
  264.  
  265.  
  266.   /* Close the Intuition Library since we have opened it: */
  267.   CloseLibrary( IntuitionBase );
  268.  
  269.   /* THE END */
  270. }
  271.  
  272. /*************************************************************************/
  273. /* EXTRA INFORMATION:                                                    */
  274. /* We will recieve a message (GADGETDOWN) when the user selects the      */
  275. /* knob, and one message (GADGETUP) when the user releases the knob. If  */
  276. /* the user on the other hand clicks inside the container (not on the    */
  277. /* knob) we will recieve both a GADGETDOWN and a GADGETUP message at the */
  278. /* same time.                                                            */
  279. /* It is because of that we need to have a while loop which collects the */
  280. /* messages once one or more has arrived. We can not as before just wait */
  281. /* and then collect one message, since there may be more in the queue.   */
  282. /*************************************************************************/
  283.